<?php
if (!defined('IN_WONDERSHARE'))
{
die('Hacking attempt');
}
require('./classes/module/module_faq_comment.php');
class FaqCommentControl
{
	var $faq_comment_m;
	function __construct()
	{
	
		$this->faq_comment_m = new FaqCommentModule();
	}

	/**
	* 根据id得到faq_comment信息
	*
	* @param   int         $comment_id
	*
	* @return  array
	*/
	function  get_comment_id_info($comment_id)
	{
		$sql = "SELECT com_id ,new_id,com_content,com_type ,com_useful ,title,com_rating ,com_status 
		,com_name,com_email  FROM `faq_commet`	 WHERE `com_id` = '$comment_id' AND `is_delete`=0";
		return $this->faq_comment_m->get_comment_info($sql);
	}
	
	/**
	* 增加或者修改comment
	*
	* @param   string      $type            操作类型
	* @param   array       $operate_array     操作数组
	* @return  int
	*/
	function  operate_comment($type =  '' ,$operate_array = null)
	{
		if($type == 'add')
		{
			$sql ="INSERT INTO faq_commet (new_id,com_content,com_type,com_useful,com_rating,com_status,update_time,update_user,title ,com_name,com_email) 
			VALUES ('{$operate_array['new_id']}','{$operate_array['com_content']}','{$operate_array['com_type']}','{$operate_array['com_useful']}',
			'{$operate_array['com_rating']}','{$operate_array['com_status']}',UNIX_TIMESTAMP(),'".$_SESSION['AName']."','{$operate_array['title']}',
			'{$operate_array['name']}','{$operate_array['email']}') ";
			$GLOBALS['db']->query($sql);
			return $GLOBALS['db']->insert_id();
		}
		elseif($type == 'edit')
		{
			$sql = " UPDATE faq_commet SET new_id = '{$operate_array['new_id']}',com_content = '{$operate_array['com_content']}',
			com_type = '{$operate_array['com_type']}',com_useful = '{$operate_array['com_useful']}',com_rating = '{$operate_array['com_rating']}',
			com_status = '{$operate_array['com_status']}',update_time = UNIX_TIMESTAMP(),update_user = '".$_SESSION['AName']."',title = '{$operate_array['title']}'
			,com_name = '{$operate_array['name']}',com_email = '{$operate_array['email']}'   WHERE com_id = ".$operate_array['com_id']." ";
			
		}
		return $this->faq_comment_m->change_info($sql);	 	
	}
	/**
	* 批量删除评论
	*
	* @param   string         $list_id    id字符串
	* @return  int
	*/
	function delete_comment($list_id)
	{
		$sql = "UPDATE `faq_commet` SET `is_delete`=1  WHERE FIND_IN_SET(com_id,'".$list_id."') ";
		return $this->faq_comment_m->change_info($sql);
	}

	/**
	* 得到分页相关信息
	*
	* @param   int        $page       页码
	* @param   int        $type       为1代表没有条件查询 为2代表有条件查询
	* @param   INT        $webType    网站类型
	* @return  array
	*/
	function get_url_info($page,$type,$pagesize, $webType = 1 ,$search_array = null)
	{
		$url_str = '';
		if($type == 'list')
		{ 
			$sql = 'SELECT COUNT(*) AS total FROM faq_commet WHERE  is_delete = 0 AND web_type = '.$webType;
		}else if($type == 'faq_list')
		{
			$where_new = $where_help = '';
			$data = $this->get_search_condition($search_array,'faq_list');
			$url_str = $data['url_str'];
			
			if($search_array['faq_type'] == 1)
			{
				$where_help = ' WHERE 1!=1 ';
				$where_new = $data['where_str'];
			}else if($search_array['faq_type'] == 2 || $search_array['faq_type'] == 3 )
			{
				$where_new = ' WHERE 1!=1 ';
				$where_help = empty($data['where_str'])?' HAVING type = '.$search_array['faq_type']:$data['where_str']. ' AND type = '.$search_array['faq_type'] ;
			}
	
			$sql = 'SELECT COUNT(*) AS total FROM  ((SELECT 1 AS type,title FROM faq_info_new '.$where_new.'  ) UNION ALL 
			(SELECT IF(is_video = 0,3,2) AS type,title FROM  online_help '.$where_help.'   )) AS nums  ' ;
		}
		else
		{
			$data = $this->get_search_condition($search_array,'get_url');
			$url_str = $data['url_str'];
			$sql = "SELECT COUNT(*) AS total FROM faq_commet WHERE  `is_delete`=0 ".$data['where_str']." AND web_type = $webType   ";
		}
		
		$page_info = get_page_info($sql,$pagesize,$page);
		$page_url = get_page_url($page,$page_info['page_num'],$url_str);
		
		return  array('page_info'=>$page_info,'page_url'=>$page_url);
	}
	/**
	* 得到列表comment信息
	*
	* @param   string     $type       类型（list为列表 不带搜索 而 list_s 在列表 带有搜索）
	* @param   int        $start      从那行开始
	* @param   int        $pagesize   取多少条记录
	* @param   INT        $webType    网站类型
	* @param   array        $search_array   搜索数组
	* @return  array
	*/
	function  get_comment_info($type,$start = 0,$pagesize = 10,$webType = 1,$search_array = null)
	{
	
		if($type == 'list')
		{
			$sql = " SELECT com_id,new_id,com_type ,com_useful,com_rating,com_name,com_email,com_content,
			IF(com_time > 0, FROM_UNIXTIME(com_time, '%Y-%m-%d %H:%s'), '') com_time,title,
			IF(update_time > 0, FROM_UNIXTIME(update_time, '%Y-%m-%d %H:%s'), '') update_time FROM 
			faq_commet  WHERE is_delete = 0 AND web_type =  $webType   ORDER BY com_id  DESC  LIMIT $start,$pagesize ";
		}else
		{
			$data = $this->get_search_condition($search_array);
			$sql = "SELECT com_id,new_id,com_type ,com_useful,com_rating,com_name,com_email,com_content,
			IF(com_time > 0, FROM_UNIXTIME(com_time, '%Y-%m-%d %H:%s'), '') com_time, title,
			IF(update_time > 0, FROM_UNIXTIME(update_time, '%Y-%m-%d %H:%s'), '') update_time FROM 
			faq_commet  WHERE is_delete = 0  AND web_type =  $webType   ".$data['where_str']."  ORDER BY com_id  DESC  LIMIT $start,$pagesize ";
			
		
		}
		return $this->faq_comment_m->get_comment_info($sql);
	}
	/**
	* 得到所有FAQ信息（视频信息 ，在线帮助，文章FAQ）
	* @param   int        $start      从那行开始
	* @param   int        $pagesize   取多少条记录
	* @param   INT        $webType    网站类型
	* @return  array
	*/
	function  get_faq_list($start = 0,$pagesize = 10, $webType = 1,$search_array = null)
	{
		$where_new = $where_help = '';
		$data = $this->get_search_condition($search_array,'faq_list');
		if($search_array['faq_type'] == 1)
		{
			$where_help = ' WHERE 1!=1 ';
			$where_new = $data['where_str'] ;
		}else if($search_array['faq_type'] == 2 || $search_array['faq_type'] == 3 )
		{
			$where_new = ' WHERE 1!=1 ';
			$where_help = empty($data['where_str'])?' HAVING type = '.$search_array['faq_type']:$data['where_str']. ' AND type = '.$search_array['faq_type'] ;
		}

		$sql = 'SELECT * FROM ((SELECT id,title,1 AS type FROM faq_info_new '.$where_new.' ) UNION ALL 
		(SELECT id,title,IF(is_video = 0,3,2) AS type FROM  online_help '.$where_help.' ) ORDER BY  type ASC ) faq_info 
		LIMIT '.$start.','.$pagesize.' ' ;
		
		return $this->faq_comment_m->get_comment_info($sql);
	}
	/**
	* 得到在搜索的条件
	*
	* @param   array        $search_array   搜索条件
	* @return  array
	*/
	function get_search_condition($search_array,$type = '')
	{
		$url_str = '&url_page=1'; $where_str = '';
		if($type == 'faq_list')
		{
			if(!empty($search_array['faq_title']))
			{
				$where_str = ' HAVING  title LIKE "%'.$search_array['faq_title'].'%" ';
				$url_str = '&faq_title='.urlencode($search_array['faq_title']);
			
			}
			$url_str .= '&faq_type='.$search_array['faq_type'];
			return array('where_str' => $where_str,'url_str' => $url_str);
		}else
		{
			if(!empty($search_array['com_id']))
			{
				$where_str = ' AND com_id = '.(int)$search_array['com_id'];
				if($type == 'get_url')$url_str = '&url_page=1&co_id='.(int)$search_array['com_id'];
			}else
			{
				if(!empty($search_array['com_type']))
				{
				    $where_str = ' AND com_type = '.(int)$search_array['com_type'];
				    if($type == 'get_url')$url_str = '&url_page=1&co_type='.(int)$search_array['com_type'];
				}
				if($search_array['com_content'] != 'all')
				{
				    if($search_array['com_content'] == 0)
				    {
				       $where_str .= ' AND com_content  = "" ';	
				    }else
				    {
				        $where_str .= ' AND com_content  != ""  ';
				    }
				    $url_str .= '&co_content='.$search_array['com_content'];
				}
				if(!empty($search_array['com_title']))
				{
				    $where_str .= " AND title LIKE '%{$search_array['com_title']}%'";
				    $url_str.='&co_title='.urlencode($search_array['com_title']);
				}
				if(!empty($search_array['start_date']))
				{
				    $where_str .= ' AND com_time >=  '.$search_array['start_date'];
				    $url_str.='&start_date='.$search_array['start_date'];
				}
				if(!empty($search_array['end_date']))
				{
				    $where_str .= ' AND com_time <= '.$search_array['end_date'];
				    $url_str.='&end_date='.$search_array['end_date'];
				}
				if($type == 'get_url')$url_str .= '&co_state='.$search_array['com_status'];
			}
			if($type == 'get_url')
			{
				return array('where_str' => $where_str,'url_str' => $url_str);
			}else
			{
				return array('where_str' => $where_str);
			}
		}
	
	}

}
?>